package com.gestordeavisos;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public class NuevoAvisoServlet extends HttpServlet {
    
	private static final Logger log = Logger.getLogger(NuevoAvisoServlet.class.getName());
    
	@SuppressWarnings("null")
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
    throws IOException {
    	UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();
        String actividad = req.getParameter("actividad");
        String cuanto = req.getParameter("cuanto");
        ArrayList<String> cuando = new ArrayList<String>();
        for(int i=0;i<Integer.valueOf(cuanto);i++){
            cuando.add(req.getParameter("cuando"+i));
        }
        String fecha = req.getParameter("fecha");
        String repeticion=req.getParameter("repeticion");
        Aviso aviso = new Aviso(user,actividad,cuanto, cuando,fecha,repeticion);
        
        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(aviso);
        } finally {
            pm.close();
        }
        resp.sendRedirect("/gestordeavisos.jsp");
    }


}
